package Controller;

import Service.MenuService;
import domain.Menu;
import domain.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;


        /*
            查询所有菜单列表
        */

        @RequestMapping("/findAllMenu")
        public ResponseResult findAllMenu(){

            List<Menu> menu = menuService.findAllMenu();

            return new ResponseResult(true,200,"查询所有菜单成功",menu);

        }

        /*
            回显菜单信息
         */
        @RequestMapping("/findMenuInfoById")
        public ResponseResult findMenuInfoById(int id){

            if(id != -1){

                // 修改操作 ---> 回显信息中就 需要回显父级菜单名称
                Menu menu = menuService.findMenuById(id);
                List<Menu> list = menuService.findSubMenuListByPid(-1);

                // 封装数据 ---> 显示所有父级信息 和 子级信息
                HashMap<String, Object> map = new HashMap<>();
                map.put("menuInfo",menu);
                map.put("parentMenuList",list);

                return new ResponseResult(true,200,"修改菜单数据成功",map);




            }else{

                // 新增操作 ----> 回显信息中就 不需要回显父级菜单名称
                List<Menu> subMenuListByPid = menuService.findSubMenuListByPid(-1);

                //封装数据 此时只需要显示 父级菜单就可以
                HashMap<String, Object> map = new HashMap<>();
                map.put("menuInfo",null);
                map.put("parentMenuList",subMenuListByPid);

                return new ResponseResult(true,200,"新增菜单数据成功",map);


            }

        }


}
